// index.js
// 获取应用实例
const app = getApp();
import httpUtil from '../../utils/httpUtil'
import pageHelpUtil from '../../utils/pageHelpUtil'
import Toast from '@vant/weapp/toast/toast';
Page({
  pageUtil:new pageHelpUtil('pageContent'),
  pageUtil2:new pageHelpUtil('pageContent2'),
  pageUtil3:new pageHelpUtil('pageContent3'),
  data: {
    pageContent:{
      list:[],
      pageNum:1,
      loading:true,
      loaded:true,
      total:0
    },
    pageContent2:{
      list:[],
      pageNum:1,
      loading:true,
      loaded:true,
      total:0
    },
    pageContent3:{
        list:[],
        pageNum:1,
        loading:true,
        loaded:true,
        total:0
      },
    picker:true,
    message:'',
    checked: true,
    columns: ['下单后3分钟', '下单后4分钟', '下单后5分钟', '下单后6分钟', '下单后7分钟',
     '下单后8分钟', '下单后9分钟', '下单后10分钟', '下单后11分钟', '下单后12分钟', '下单后13分钟'
     , '下单后14分钟', '下单后15分钟', '下单后16分钟', '下单后17分钟'],
  },
  renew(e){
      var storeId = e.currentTarget.dataset.id
      wx.navigateTo({
        url: '../charge/charge?storeId='+storeId  // 跳转到对应的页面路径
    })
  },
  onPullDownRefresh(){
    console.log('下拉刷新了')
    this.setData({
        pageContent:{
            list:[],
            pageNum:1,
            loading:true,
            loaded:true,
            total:0
          },
          pageContent2:{
            list:[],
            pageNum:1,
            loading:true,
            loaded:true,
            total:0
          },
          pageContent3:{
              list:[],
              pageNum:1,
              loading:true,
              loaded:true,
              total:0
            },
    })
    this.onLoad()
    wx.stopPullDownRefresh()
  },
  formatDate(date) {
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    return `${year}-${month}-${day}`;
  },
  toPicker(e){

      console.log(e)
      this.setData({
          picker:!this.data.picker
      })
      var storeId = e.currentTarget.dataset.id
      var itemIndex = e.currentTarget.dataset.index
      this.setData({
          pickStoreId:storeId,
          itemIndex: itemIndex
      })

  },
  // 事件处理函数
  onConfirm(event) {
    var that = this;
    const { picker, value, index } = event.detail;
    this.setData({
        picker:!this.data.picker
    })
    console.log(`当前值：${value}, 当前索引：${index}`);
    var storeId = this.data.pickStoreId
    console.log('storeId='+storeId)
    httpUtil.getRequest("/api/rule/update",{
        time:index+3,
        storeId:storeId
    }).then(res =>{
        console.log(this.data.itemIndex)
        var pageContent = that.data.pageContent
        pageContent.list[this.data.itemIndex].time=index+3
        that.setData({
            pageContent:pageContent
        })
        wx.showToast({
            title: '设置成功',
            icon: 'success',
            duration: 2000
          })
    })
  },
  onChangeOut({detail ,currentTarget}){
    var that = this;
    console.log(currentTarget)
    console.log(detail)
    var storeId = currentTarget.dataset.id;
    var itemIndex = currentTarget.dataset.index;
    console.log('indexindex='+itemIndex)
    httpUtil.getRequest('/api/store/outSet',{
        'storeId':storeId,
        'autoOut':detail?'TRUE':'FALSE'
    }).then(res=>{
      var pageContent = that.data.pageContent
      pageContent.list[itemIndex].disAutoOut=detail
      that.setData({
          pageContent:pageContent
      })
      wx.showToast({
          title: '设置成功',
          icon: 'success',
          duration: 2000
        })
    })
  },
  onChange({ detail ,currentTarget}) {
      var that = this;
      console.log(currentTarget)
      console.log(detail)
      var storeId = currentTarget.dataset.id;
      var itemIndex = currentTarget.dataset.index;
      console.log('indexindex='+itemIndex)
      httpUtil.getRequest('/api/evaluate/evaluateSet',{
          'storeId':storeId,
          'autoReplay':detail?'TRUE':'FALSE'
      }).then(res=>{
        var pageContent = that.data.pageContent
        pageContent.list[itemIndex].disAutoReplay=detail
        that.setData({
            pageContent:pageContent
        })
        wx.showToast({
            title: '设置成功',
            icon: 'success',
            duration: 2000
          })
      })
    // 需要手动对 checked 状态进行更新

    this.setData({ checked: detail });
  },
  kefu(){
    wx.navigateTo({
        url: '../kefu/kefu'
    })
  },
  tobind(){
    httpUtil.getRequest('/api/user/getisTesting',{}).then(res =>{
        console.log(res)
        if(res){
            wx.navigateTo({
                url: '../bindtest/bindtest'
            })
        }else{


              wx.navigateTo({
                  url: '../loginselect/loginselect'
              })
        }
    })

  },
  toReLogin(e){
    var storeId = e.currentTarget.dataset.id
    var platform = e.currentTarget.dataset.platform
    console.log(platform)
    if('ELEME' == platform){
        wx.navigateTo({
            url: '../tobindeleme/tobindeleme?storeId='+storeId
        })
    }else{
        wx.navigateTo({
            url: '../relogin/relogin?storeId='+storeId
        })
    }

  },
  onCancel() {
    this.setData({
        picker:!this.data.picker
    })
    console.log('取消');
  },


  notice(){
      var that = this;
        httpUtil.getRequest('/api/notice/get',{

        }).then(res=>{
            that.setData({
                message:res.message
            })
        })
  },

  onLoad(options) {

    var that = this;
    new Promise((resolve,reject) =>{
      var token = getApp().globalData.token
      console.log(token)
      if(token != null && token != ''){
        resolve(token)
      }else{
        app.isLoginWatch(function(e){
            console.log("get login callback...")
            resolve(e)
          });
      }

    }).then(res =>{
      //设置推荐人
      try {
        this.notice()
        var referenceId = options.referenceId;
        console.log("referenceId"+referenceId)
        if(referenceId != 'undefined' && referenceId != null && referenceId != ''){
          console.log('开始设置推荐人'+referenceId)
          that.setFeference(referenceId)
        }
      }catch(e){
          
      }finally{
        console.log('结束')
      }

      that.pageUtil.load( ()=>  httpUtil.getRequest("/api/store/storeList",{
        pageNum:that.data.pageContent.pageNum,
        onlineStatus:'ONLINE',
        storeStatus:1,
        pageSize:100,
      }),that,'pageContent')
      that.pageUtil2.load( ()=>  httpUtil.getRequest("/api/store/storeList",{
        pageNum:that.data.pageContent2.pageNum,
        onlineStatus:'OFF',
        storeStatus:1,
        pageSize:100,
      }),that,'pageContent2')
      that.pageUtil3.load( ()=>  httpUtil.getRequest("/api/store/storeList",{
        pageNum:that.data.pageContent3.pageNum,
        storeStatus:2,
        pageSize:100,
      }),that,'pageContent3')
    })

  },
  guanzhu(){
    wx.navigateTo({
      url: '../test/test' // 跳转到对应的页面路径
  })
  },
setFeference(referenceId){
    httpUtil.getRequest("/api/user/setReferenceId",{
      'referenceId':referenceId
    }).then(res =>{
      console.log('推荐人设置成功')
    })
},
onShareAppMessage: function () {
  console.log('/pages/index/index?referenceId='+getApp().globalData.userId,)
    return {
      title: '美团饿了么自动出餐',
      path: '/pages/index/index?referenceId='+getApp().globalData.userId,
      imageUrl: '../../image/fx.jpg'
    }




  
},
  // fx(){
  //   wx.shareFileMessage({
  //     filePath: 'filePath',
  //   })({ //分享给朋友
  //     path: '../../image/fx.jpg',
  //     success: (res) => {
  //         console.log("分享成功:", res);
  //     },
  //     fail: (err) => {
  //         console.log("分享取消:", err);
  //     },
  // })
  // },
  toRecord(e){

        console.log(e)
        var storeId = e.currentTarget.dataset.id
        console.log(storeId)
        wx.navigateTo({
            url: '../record/record?storeId='+storeId  // 跳转到对应的页面路径
        })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
    /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.pageUtil.onReachBottom(this);
  },
})
